package com.jmu.edu.IMApplication.adapter;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.jmu.edu.IMApplication.MyApplication;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.fragment.ChatFragment;
import com.jmu.edu.IMApplication.utils.MsgUtils;
import com.jmu.edu.IMApplication.utils.ViewUtils;
import com.jmu.edu.IMApplication.utils.WillowUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.enums.ContentType;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.model.UserInfo;

public class MessageItemAdapter extends RecyclerView.Adapter<MessageItemAdapter.MyViewHolder> {
    private List<Conversation> conversationList = new ArrayList<>();

    public void setConversationList(List<Conversation> conversationList) {
        this.conversationList = conversationList;
    }

    public List<Conversation> getConversationList() {
        return conversationList;
    }

    @NonNull
    @Override
    public MessageItemAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_message_list_item, parent, false);
        return new MessageItemAdapter.MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull MessageItemAdapter.MyViewHolder holder, int position) {
        Conversation conversation = conversationList.get(position);
        UserInfo userInfo = (UserInfo) conversation.getTargetInfo();
        userInfo.getAvatarBitmap(new GetAvatarBitmapCallback() {
            @Override
            public void gotResult(int i, String s, Bitmap bitmap) {
                if (bitmap != null) {
                    Glide.with(holder.avatarImage).load(bitmap).into(holder.avatarImage);
                } else {
                    Glide.with(holder.avatarImage).load(R.drawable.smc).into(holder.avatarImage);
                }
            }
        });
        holder.nicknameText.setText(WillowUtils.getNickName(userInfo));
        // 获取最后一条信息
        Message message = conversation.getLatestMessage();
        if (message != null) {
            //获取最后一条信息的发送时间，格式为 yyyy-MM-dd hh:mm:ss
            Timestamp timestamp = new Timestamp(conversation.getLatestMessage().getCreateTime());
            String dateString = timestamp.toString().substring(0, timestamp.toString().indexOf("."));
            holder.recordTimeText.setText(dateString);
            //判断信息的种类，根据种类展示不一样的内容
            if (message.getContentType() == ContentType.image) {
                holder.recordText.setText("[图片]");
            } else if (message.getContentType() == ContentType.text) {
                TextContent textContent = (TextContent) message.getContent();
                holder.recordText.setText(textContent.getText());
            }
        } else {
            holder.recordTimeText.setText("");
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tag = "chat";
                ChatFragment chatFragment = (ChatFragment) MyApplication.fragmentManager.findFragmentByTag(tag);
                if (chatFragment == null) {
                    chatFragment = new ChatFragment();
                }
                //传递必须的参数
                Bundle bundle = new Bundle();
                bundle.putString("username", userInfo.getUserName());
                bundle.putString("friendName", WillowUtils.getNickName(userInfo));
                bundle.putString("state", "在线");
                chatFragment.setArguments(bundle);

                //这个传头像暂时是用不到了
                //chatFragment.setFriendAvatar(holder.avatar);
                //跳转到聊天界面
                ViewUtils.showFragment(chatFragment, tag, R.anim.from_right, R.anim.out_right);
            }
        });

    }

    @Override
    public int getItemCount() {
        return conversationList.size();
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView avatarImage;
        private TextView nicknameText, recordText, recordTimeText;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            avatarImage = itemView.findViewById(R.id.fragment_message_friend_avatar);
            nicknameText = itemView.findViewById(R.id.fragment_message_friend_nickname);
            recordText = itemView.findViewById(R.id.fragment_message_friend_last_chat_record);
            recordTimeText = itemView.findViewById(R.id.fragment_message_friend_chat_last_record_time);
        }
    }
}
